package com.start.common.api;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;

/**
 * 统一返回结果封装
 *
 * @param <T>
 */
public class Result<T> extends HashMap<String, Object> {
    public final static int CODE_OK = 0;//业务处理成功
    public final static int CODE_FAIL = 500;//业务异常
    public final static int CODE_401 = 401;//用户未登录
    public final static int CODE_403 = 403;//未授权
    public final static int CODE_DEADLY = -1;//致命异常
    public final static int CODE_NOT_EXISTENT = 404;//资源不存在
    public final static String KEY_CODE = "code";
    public final static String KEY_SUCCESS = "success";
    public final static String KEY_DATA = "data";
    public final static String KEY_OTHER_CODE = "otherCode";
    public final static String KEY_MESSAGE = "message";



    public int getCode() {
        return (int) getOrDefault(KEY_CODE, 0);
    }

    public void setCode(int code) {
        super.put(KEY_CODE, code);
    }

    public String getMessage() {
        return (String) super.getOrDefault(KEY_MESSAGE, null);
    }

    public void setMessage(String message) {
        super.put(KEY_MESSAGE, message);
    }

    public Boolean getSuccess() {
        return (Boolean) super.getOrDefault(KEY_SUCCESS, null);
    }

    public void setSuccess(Boolean success) {
        super.put(KEY_SUCCESS, success);
    }

    /**
     * 数据
     *
     * @return T
     */
    public T getData() {
        if (!super.containsKey(KEY_DATA)) {
            return null;
        }
        final Object o = super.get(KEY_DATA);
        if (o == null) return null;
        return (T) o;
    }

    public void setData(T data) {
        super.put(KEY_DATA, data);
    }

    /**
     * 其他编码
     *
     * @return otherCode
     */
    public int getOtherCode() {
        return (int) super.getOrDefault(KEY_OTHER_CODE, 0);
    }

    public void setOtherCode(int otherCode) {
        super.put(KEY_OTHER_CODE, otherCode);
    }

    /**
     * 未认证
     *
     * @param message 未认证
     * @return Result
     */
    public static Result<?> fail401(String message) {
        Result<Object> result = new Result<>();
        result.setCode(CODE_401);
        result.setMessage(message);
        return result;
    }

    /**
     * 无权限访问
     *
     * @return Result
     */
    public static Result<?> fail403() {
        Result<Object> result = new Result<>();
        result.setCode(CODE_403);
        result.setMessage("无权限访问");
        return result;
    }

    /**
     * 访问404
     *
     * @param msg 错误消息
     * @return Result
     */
    public static Result<?> fail404(String msg) {
        Result<Object> result = new Result<>();
        result.setCode(CODE_NOT_EXISTENT);
        result.setMessage(StringUtils.isEmpty(msg) ? "资源不存在" : msg);
        return result;
    }

    /**
     * 访问成功
     *
     * @return Result
     */
    public static Result<?> ok() {
        Result<Object> result = new Result<>();
        result.setCode(CODE_OK);
        result.setSuccess(true);
        return result;
    }

    /**
     * 访问成功
     *
     * @param data 数据内容
     * @param <T>  数据类型
     * @return Result
     */
    public static <T> Result<?> ok(T data) {
        Result<T> result = new Result<>();
        result.setCode(CODE_OK);
        result.setSuccess(true);
        result.setData(data);
        return result;
    }

    /**
     * 访问成功
     *
     * @param message 消息内容
     * @param data    返回数据
     * @param <T>     数据类型
     * @return Result
     */
    public static <T> Result<T> ok(String message, T data) {
        Result<T> result = new Result<T>();
        result.setCode(CODE_OK);
        result.setSuccess(true);
        result.setData(data);
        result.setMessage(message);
        return result;
    }

    /**
     * 访问失败
     *
     * @param message 失败消息
     * @return Result
     */
    public static Result<?> fail(String message) {
        Result<Object> result = new Result<>();
        result.setCode(CODE_FAIL);
        result.setSuccess(true);
        result.setMessage(message);
        return result;
    }

    /**
     * 失败
     *
     * @param message 错误消息
     * @param success 服务访问是否成功
     * @return Result
     */
    public static Result<?> fail(String message, Boolean success) {
        Result<Object> result = new Result<>();
        result.setCode(CODE_FAIL);
        result.setSuccess(success);
        result.setMessage(message);
        return result;
    }

    /**
     * 系统致命异常
     *
     * @return Result
     */
    public static Result<?> deadly() {
        Result<Object> result = new Result<>();
        result.setCode(CODE_FAIL);
        result.setSuccess(false);
        result.setMessage("系统致命异常请联系管理");
        return result;
    }
}
